DAY3:Jaden Casting Strings


Posted by birdbirdmurmur on 2023-07-16

題目連結:

Jaden Casting Strings

解題過程:

  • 取得每個單字的第一個字
  • slice()後的陣列很特別
    • 可以暫時儲存一個陣列(slice array)
  • 把第一個字轉成大寫
    • toUpperCase()會把整個string都轉換
  • 把大寫字和原本的單字合併
  • 反過來做一次迴圈
  • 回傳

解法:

String.prototype.toJadenCase = function () {
  const words = this.split(' ')
  const answer = words.map( (word) =>{
    const first = word.charAt(0).toUpperCase();
    const rest = word.slice(1);
    return first + rest;
  })
  return answer.join(' ')
};

延伸思考:

對字串還沒有很熟練
一直翻MDN資料來看
但越來越懂string和array之間的關係了!


#javascript #Codewars #split #map #charat #toUpperCase #slice #join







Related Posts

React 基礎:環境建置篇

React 基礎:環境建置篇

讀書筆記-版本控制使用Git: 合併

讀書筆記-版本控制使用Git: 合併

[7] 進階資料型別 part2 - Tuple

[7] 進階資料型別 part2 - Tuple


Comments